<?php

/* ==================================== by maluda1988 =============================== */

function m_test(){
	echo 1;
}
function m_upload(){
	 $upload = new \Think\Upload();// 实例化上传类
	 $upload->maxSize = 3145728 ;// 设置附件上传大小
	 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	 $upload->rootPath = './data/upload/'; // 设置附件上传根目录
	 $upload->savePath = ''; // 设置附件上传（子）目录
	 // 上传文件
	  $info = $upload->upload();
	  if(!$info) {// 上传错误提示错误信息
		  $this->error($upload->getError());
	  }else{// 上传成功
		  foreach ($info as $file){
			   $uploadname=$upload->rootPath.$file['savepath'].$file['savename'];
		  }
		  return $uploadname;
	  }
}

function m_page($pageBtn=7,$onePageNum=10,$M="ecms_homemb",$where,$order="id desc"){
	//$pageBtn;	//选择页面数
	//$onePageNum;	//每页显示的条数
	$total=M($M)->where($where)->count();//数据条数
	$pageNum=ceil($total/$onePageNum);//页面数
	if($pageNum<$pageBtn){	//页面数小于选择页面数
		$pageBtn=$pageNum;
	}
	if($pageBtn%2==0){	//选择页面数为偶数时
		$even=1;
	}else{
		$even=0;
	}
	$pageFloor=floor($pageBtn/2);	//左右偏移量
	if(isset($_GET['page'])){
		$page=$_GET['page'];
	}else{
		$page=1;
	}
	$data=M($M)->where($where)->page($page,$onePageNum)->order($order)->select();
	$pageShow=' <div class="pagination"><span class="current" >'.$page.' / '.$pageNum .'</span>';
	if($page!=1){
		$pageShow.=' <a  href="'.U('index?page=1').'">首页</a>';
		$pageShow.=' <a  href="'.U('index?page=').($page-1).'">上一页</a>';
	}
	if($page<=$pageFloor){
		$pageLeft=1;
		$pageRight=$pageBtn;
	}
	else if($page<=$pageNum-$pageFloor){
		$pageLeft=$page-$pageFloor+$even;
		$pageRight=$page+$pageFloor;
	}else{
		$pageLeft=$page-(2*$pageFloor)+($pageNum-$page)+$even;
		$pageRight=$pageNum;
	}
	for($i=$pageLeft;$i<=$pageRight;$i++){
		if($i==$page){
			$pageShow.=' <span class="current" href="'.U('index?page=').$i.'">'.$i.'</span>';
		}else{
			$pageShow.=' <a  href="'.U('index?page=').$i.'">'.$i.'</a>';
		}
	}
	if($page!=$pageNum){
		$pageShow.=' <a  href="'.U('index?page=').($page+1).'">下一页</a>';
		$pageShow.=' <a  href="'.U('index?page=').$pageNum.'">尾页</a>';
	}
	$m_page['data']=$data;
	$m_page['page']=$pageShow."<a>共 ".$total." 条记录</a></div>";
	return $m_page;
}

function sp_admin_get_tpl_file_list(){
	$template_path=C("SP_TMPL_PATH").C("SP_DEFAULT_THEME")."/Shop/";
	$files=sp_scan_dir($template_path."*");
	$tpl_files=array();
	foreach ($files as $f){
		if($f!="." || $f!=".."){
			if(is_file($template_path.$f)){
				$suffix=C("TMPL_TEMPLATE_SUFFIX");
				$result=preg_match("/$suffix$/", $f);
				if($result){
					$tpl=str_replace($suffix, "", $f);
					$tpl_files[$tpl]=$tpl;
				}
			}
		}
	}
	return $tpl_files;
}
/* ==================================== by maluda1988 end  =============================== */
